/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sistema.gestionuniversitaria.presentationLayer;

import com.sistema.gestionuniversitaria.BusinessLayer.Carrera;
import com.sistema.gestionuniversitaria.BusinessLayer.Curso;
import com.sistema.gestionuniversitaria.dataLayer.CarreraData;
import com.sistema.gestionuniversitaria.dataLayer.CursoData;
import com.sistema.gestionuniversitaria.dataLayer.ExcepcionGlobal;
import com.sistema.gestionuniversitaria.dataLayer.NoDataException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Max
 */
public class MostrarHistorial extends javax.swing.JInternalFrame {

    /**
     * Creates new form MostrarHistorial
     */
    CarreraData caData = new CarreraData();
    CursoData curData = new CursoData();
    DefaultTableModel modelo = new DefaultTableModel();

    public MostrarHistorial() throws SQLException, ExcepcionGlobal, NoDataException {
        initComponents();
        jComboCarreras.removeAllItems();
        cargarListaCarreras();
    }

    private void cargarListaCarreras() throws SQLException, ExcepcionGlobal, NoDataException {
        ArrayList<Carrera> carreras = caData.getCarreras();
        for (Carrera carrera : carreras) {
            jComboCarreras.addItem(carrera);
        }
    }

    private void llenarTabla(String codCarrera) throws SQLException, ExcepcionGlobal, NoDataException {

        modelo = new DefaultTableModel();
        ArrayList<Curso> listaCursos = new ArrayList<Curso>();
        listaCursos = curData.getHistorialCursos(jTbxCedula.getText().toUpperCase(), codCarrera);
        if (listaCursos.isEmpty()) {
            JOptionPane.showMessageDialog(null, "El estudiante no tiene ningun curso en el historial");
        } else {
            modelo.setColumnCount(0);
            modelo.addColumn("Sigla");
            modelo.addColumn("Nombre");
            modelo.addColumn("Créditos");
            modelo.addColumn("Nota");
            modelo.addColumn("Estado");
            modelo.setNumRows(listaCursos.size());
            int contador = 0;
            for (Curso curso : listaCursos) {
                modelo.setValueAt(curso.getSigla(), contador, 0);
                modelo.setValueAt(curso.getNombre(), contador, 1);
                modelo.setValueAt(curso.getCreditos(), contador, 2);
                modelo.setValueAt(String.valueOf(curso.getNota()), contador, 3);
                modelo.setValueAt(curso.getEstado(), contador, 4);
                contador = contador + 1;
            }

            jTableCursos.setModel(modelo);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTbxCedula = new javax.swing.JTextField();
        jComboCarreras = new javax.swing.JComboBox();
        jBtnBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableCursos = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Mostrar Historial");
        setToolTipText("");

        jLabel1.setText("Rellene la información solicitada para efectuar la búsqueda:");

        jLabel2.setText("Cedula Alumno:");

        jLabel3.setText("Carrera:");

        jBtnBuscar.setText("Buscar");
        jBtnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnBuscarActionPerformed(evt);
            }
        });

        jTableCursos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTableCursos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jBtnBuscar)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel3))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTbxCedula)
                                .addComponent(jComboCarreras, 0, 169, Short.MAX_VALUE))))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 461, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTbxCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jComboCarreras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnBuscar)
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(48, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnBuscarActionPerformed
        Carrera carrSeleccionada = (Carrera) jComboCarreras.getSelectedItem();
        try {
            llenarTabla(carrSeleccionada.getCodigo());
        } catch (SQLException ex) {
            Logger.getLogger(MostrarHistorial.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExcepcionGlobal ex) {
            Logger.getLogger(MostrarHistorial.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoDataException ex) {
            Logger.getLogger(MostrarHistorial.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jBtnBuscarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnBuscar;
    private javax.swing.JComboBox jComboCarreras;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableCursos;
    private javax.swing.JTextField jTbxCedula;
    // End of variables declaration//GEN-END:variables
}
